/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.ad.feedsreader.actions;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.interceptor.SessionAware;
import twitter4j.Twitter;
import twitter4j.auth.AccessToken;
import ua.ad.feedsreader.mybatis.config.MyBatisSqlSessionFactory;
import ua.ad.feedsreader.mybatis.mappers.beans.Utilizador;
import ua.ad.feedsreader.mybatis.mappers.beans.UtilizadorOauth;
import ua.ad.feedsreader.mybatis.mappers.clients.UtilizadorOauthMapper;

/**
 *
 * @author sexta13
 */
public class TwitterAcceptAction extends ActionSupport implements SessionAware {

    private Map<String, Object> session;
    private String oauthToken;

    @Override
    public String execute() throws Exception {
        Utilizador userSession = (Utilizador) getSession().get("utilizador");
        
        SqlSession bdSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();


        UtilizadorOauthMapper utOauthMapper = bdSession.getMapper(UtilizadorOauthMapper.class);

        UtilizadorOauth utOauth = new UtilizadorOauth();
        Twitter tw = (Twitter)getSession().get("TWITTER");
        AccessToken accessToken = tw.getOAuthAccessToken();
                       
        
        String idTwitter = String.valueOf(tw.verifyCredentials().getId());
        utOauth.setIdUserExternalApp(idTwitter);
        utOauth.setIdTypeExtApp(2);
        utOauth.setIdUtilizador(userSession.getId());
        utOauth.setOauthCode(accessToken.getToken() +"@@@"+accessToken.getTokenSecret());
        
        utOauthMapper.insertSelective(utOauth);



        return SUCCESS;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public Map<String, Object> getSession() {
        return session;
    }

    public void setOauthToken(String oauthToken) {
        this.oauthToken = oauthToken;
    }

    public String getOauthToken() {
        return oauthToken;
    }
}
